package com.runmi.webchat.aftersale.common.page;


import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.runmi.webchat.aftersale.common.page.PageModel;

public class PageAjaxTag  extends TagSupport {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public int doStartTag() throws JspException {
		
		StringBuilder pageInfo = new StringBuilder();
		try {
		
			PageModel model=new PageModel();
			model.setCpage(cpage);
			model.setPageSize(pageSize);
			model.setIsAjax(true);
			model.setTotalCount(totalCount);
						
			JspWriter out = pageContext.getOut();		
			
			pageInfo.append("\r\n");
            if (model.getTotalPage() > 0 && model.getCpage() <=model.getTotalPage())
            {
				pageInfo.append("<div class=\"paging-class text-right\">");
				 if (model.getCpage() > 1)
		         {
		        	 pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage(1,{0})\" class=\"paging-btn paging-default\" >首页</a>",  model.getTotalPage()));
	                 pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{1})\"  class=\"paging-btn paging-default\">上一页</a>", (model.getCpage() - 1), model.getTotalPage()));
		         }else{
		        	 pageInfo.append("<a class=\"paging-btn paging-default paging-disabled\" href=\"\">首页</a>");
		        	 pageInfo.append("<a class=\"paging-btn paging-default paging-disabled\" href=\"\">上一页</a>");
		         }
                //页码第一个等于第一页 那么不显示前置的点点点
                if (model.getPageFirst() > 1)
                {
                    pageInfo.append(MessageFormat.format("<a   href=\"javascript:gotopage({0},{1})\" class=\"paging-btn num-btn\">.&nbsp;.</a>", (model.getPageFirst() - 1), model.getTotalPage()));
                }
                
                for(int i= 0; i < model.getDispPageCount(); i++)
                {
                    if ((model.getPageFirst() + i) <= model.getTotalPage())
                    {
                        if (model.getCpage() == (model.getPageFirst() + i))
                        {
                           
                        	pageInfo.append(MessageFormat.format("<a class=\"paging-btn num-btn active\"   href=\"javascript:gotopage({0},{1})\" >{0}</a>", (model.getPageFirst() + i), model.getTotalPage()));
                           
                        }
                        else
                        {
                        	pageInfo.append(MessageFormat.format("<a class=\"paging-btn num-btn\"   href=\"javascript:gotopage({0},{1})\" >{0}</a>", (model.getPageFirst() + i), model.getTotalPage()));
                        }
                    }
                }
                //第一个页码+要显示的页码数小于等于总页数 就可显示后置的点点点
                if ((model.getPageFirst() + model.getDispPageCount()) <= model.getTotalPage())
                {
                    pageInfo.append(MessageFormat.format("<a   href=\"javascript:gotopage({0},{1})\" class=\"paging-btn num-btn\">.&nbsp;.</a> ", (model.getPageFirst() + model.getDispPageCount()), model.getTotalPage()));
                }
                //当前页小于总页数 那么后页 和尾页 不可用
                if (model.getCpage() < model.getTotalPage())
                {
                	
                	
                    pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{1})\" class=\"paging-btn paging-default\" >下一页</a>", (model.getCpage() + 1), model.getTotalPage()));
                    pageInfo.append(MessageFormat.format("<a href=\"javascript:gotopage({0},{0})\"  class=\"paging-btn paging-default\">尾页</a>", model.getTotalPage()));
                }
                else
                {
                	pageInfo.append("<a disabled=\"disabled\" class=\"paging-btn paging-default paging-disabled\">下一页</a>");
                	pageInfo.append("<a disabled=\"disabled\" class=\"paging-btn paging-default paging-disabled\">尾页</a>");
                }
                

			    pageInfo.append(MessageFormat.format(" <a class=\"paginItem\" style=\"display:none\">  <input   type=\"text\"  onclick=\"this.select()\" value=\"{0}\" name=\"cpage\" id=\"cpage\" style=\"width:30px;\" /></a>", model.getCpage()));
	            pageInfo.append(MessageFormat.format(" <a class=\"paginItem\" style=\"display:none\">  <input  type=\"submit\"  value=\"Go\" id=\"mvcpager_btnchk\" onclick=\"return checkInput({0},{1})\" /> </a>", model.getTotalPage(),  0));
				
	            pageInfo.append("</div>");
            }
			
			
			
			pageInfo.append("\r\n");
			pageInfo.append("<script type=\"text/javascript\">  ");
			pageInfo.append("\r\n");
            pageInfo.append(" var totalPage = 0;    ");
            pageInfo.append("\r\n");
            pageInfo.append(" function gotopage(pageIndex, lastPage) {    ");
            pageInfo.append("\r\n");
            pageInfo.append("       totalPage = lastPage;    ");
            pageInfo.append("       postAjaxPage(pageIndex);    ");
            pageInfo.append("\r\n");
           // pageInfo.append("       document.getElementById('cpage').value = pageIndex;         ");
           // pageInfo.append("\r\n");
          //  pageInfo.append("       document.getElementById('mvcpager_btnchk').click();    ");
            pageInfo.append("\r\n");
            pageInfo.append(" }     ");
            pageInfo.append("\r\n");
            pageInfo.append(" function checkInput(totalPage, isAjax) {     ");
            pageInfo.append("\r\n");
            pageInfo.append("       totalPage = totalPage; ");
            pageInfo.append("\r\n");
            pageInfo.append("       var pageIndexStr = document.getElementById('cpage').value;    ");
            pageInfo.append("\r\n");
            pageInfo.append("       if (pageIndexStr == \"\")  {     ");
            pageInfo.append("\r\n");
            pageInfo.append("           alert('请输入页码');    ");
            pageInfo.append("\r\n");
            pageInfo.append("           document.getElementById('CurrentPageIndex').focus();    ");
            pageInfo.append("\r\n");
            pageInfo.append("           document.getElementById('CurrentPageIndex').select();    ");
            pageInfo.append("\r\n");
            pageInfo.append("           return false;    ");
            pageInfo.append("\r\n");
            pageInfo.append("       }     ");
            pageInfo.append("\r\n");
            pageInfo.append("       var r = new RegExp(\"^\\\\s*(\\\\d+)\\\\s*$\");");
            pageInfo.append("\r\n");
            pageInfo.append("       var pageIndex = parseInt(pageIndexStr);");
            pageInfo.append("\r\n");
            pageInfo.append("       if (r.test(pageIndex)) { ");
            pageInfo.append("\r\n");
            pageInfo.append("           if (RegExp.$1 < 1 || RegExp.$1 > totalPage) { ");
            pageInfo.append("\r\n");
            pageInfo.append("               alert(\"页码超出范围！\");  ");
            pageInfo.append("\r\n");
            pageInfo.append("               document.getElementById('cpage').focus(); ");
            pageInfo.append("\r\n");
            pageInfo.append("               document.getElementById('cpage').select();  ");
            pageInfo.append("\r\n");
            pageInfo.append("               return false; ");
            pageInfo.append("\r\n");
            pageInfo.append("           } ");
            pageInfo.append("\r\n");
            if(model.getIsAjax()){
            	 pageInfo.append("               postAjaxPage(pageIndex); ");
                 pageInfo.append("\r\n");
            	 pageInfo.append("               return false; ");
                 pageInfo.append("\r\n");
            }else{
            	 pageInfo.append("               return true; ");
                 pageInfo.append("\r\n");
                 pageInfo.append("\r\n");
            }
////            pageInfo.append("           if (isAjax) { ");
////            pageInfo.append("               PostAjaxPage(pageIndex); ");
////            pageInfo.append("               return false; ");
////            pageInfo.append("           } else { ");
////            pageInfo.append("               return true; ");
////            pageInfo.append("           } ");
            pageInfo.append("        } ");
            pageInfo.append("       alert(\"页码不是有效的数值！\");  ");
            pageInfo.append("\r\n");
            pageInfo.append("       document.getElementById('cpage').focus(); ");
            pageInfo.append("\r\n");
            pageInfo.append("       document.getElementById('cpage').select(); ");
            pageInfo.append("\r\n");
            pageInfo.append("       return false; ");
            pageInfo.append("\r\n");
            pageInfo.append("   } ");
            pageInfo.append("\r\n");
            if(model.getIsAjax()){
	            pageInfo.append("   function postAjaxPage(pageIndex){ ");
	            pageInfo.append("\r\n");
	            pageInfo.append("       var data='&cpage=' + pageIndex ;                "); 
	            pageInfo.append("\r\n");

	            pageInfo.append("       getAjaxPageData(data)        ");
	            pageInfo.append("\r\n");
	            pageInfo.append("   } ");
            }
            pageInfo.append("  </script>");
            pageInfo.append("\r\n");
            
            
        
//
//
//	            if (model.getTotalPage() > 0 && model.getCpage() <=model.getTotalPage())
//	            {
//	            	
//	            	 pageInfo.append("<div class=\"message\">共<i class=\"blue\">" + model.getTotalCount() + "</i>条记录，当前显示第&nbsp;<i class=\"blue\">"+model.getCpage()+ "&nbsp;</i>页</div>");    
//	            	 pageInfo.append(" <ul class=\"paginList\">");    
//	            	
////	            	<ul class="paginList">
////			        <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
////			        <li class="paginItem"><a href="javascript:;">1</a></li>
////			        <li class="paginItem current"><a href="javascript:;">2</a></li>
////			        <li class="paginItem"><a href="javascript:;">3</a></li>
////			        <li class="paginItem"><a href="javascript:;">4</a></li>
////			        <li class="paginItem"><a href="javascript:;">5</a></li>
////			        <li class="paginItem more"><a href="javascript:;">...</a></li>
////			        <li class="paginItem"><a href="javascript:;">10</a></li>
////			        <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
////			        </ul>
//	            	
//	                pageInfo.append("<div id=\"mvcpager\">");    
//	                //当前页大于第一页 那么首页 前一页可用
//	                if (model.getCpage() > 1)
//	                {
//	                	//pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a href=\"javascript:gotopage({0},{1})\"><span class=\"pagepre\"></span></a></li>", (model.getCpage() - 1), model.getTotalPage()));
//	                	// MessageFormat.format("我是{0},我来自{1},今年{2}岁", "中国人", "北京", "22");
//	                	pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a  href=\"javascript:gotopage(1,{0})\" >首页</a></li>",  model.getTotalPage()));
//	                    pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a href=\"javascript:gotopage({0},{1})\" >前页</a></li>", (model.getCpage() - 1), model.getTotalPage()));
//	                }
//	                else
//	                {
//	                	//pageInfo.append("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\"></span></a></li>");
//	                	pageInfo.append("<li class=\"paginItem\"><a disabled=\"disabled\" >首页</a></li>");
//	                	pageInfo.append("<li class=\"paginItem\"><a disabled=\"disabled\" >前页</a></li>");
//	                }
//
//	                //页码第一个等于第一页 那么不显示前置的点点点
//	                if (model.getPageFirst() > 1)
//	                {
//	                    pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a   href=\"javascript:gotopage({0},{1})\" style=\"text-decoration: none;\">.&nbsp;.</a></li>", (model.getPageFirst() - 1), model.getTotalPage()));
//	                }
//	                //显示页码 dispageCount :显示的页码数
//	                for(int i= 0; i < model.getDispPageCount(); i++)
//	                {
//	                    if ((model.getPageFirst() + i) <= model.getTotalPage())
//	                    {
//	                        if (model.getCpage() == (model.getPageFirst() + i))
//	                        {
//	                           
//	                                pageInfo.append(MessageFormat.format("<li class=\"paginItem current\"><a    href=\"javascript:gotopage({0},{1})\" >{0}</a></li>  ", (model.getPageFirst() + i), model.getTotalPage()));
//	                           
//	                        }
//	                        else
//	                        {
//	                            pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a    href=\"javascript:gotopage({0},{1})\">{0}</a> </li> ", (model.getPageFirst() + i), model.getTotalPage()));
//	                        }
//	                    }
//	                }
//	                //第一个页码+要显示的页码数小于等于总页数 就可显示后置的点点点
//	                if ((model.getPageFirst() + model.getDispPageCount()) <= model.getTotalPage())
//	                {
//	                    pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a   href=\"javascript:gotopage({0},{1})\" style=\"text-decoration: none;\">.&nbsp;.</a></li> ", (model.getPageFirst() + model.getDispPageCount()), model.getTotalPage()));
//	                }
//	                //当前页小于总页数 那么后页 和尾页 不可用
//	                if (model.getCpage() < model.getTotalPage())
//	                {
//	                    pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a href=\"javascript:gotopage({0},{1})\" >后页</a> </li>", (model.getCpage() + 1), model.getTotalPage()));
//	                    pageInfo.append(MessageFormat.format("<li class=\"paginItem\"><a href=\"javascript:gotopage({0},{0})\" >尾页</a> </li>", model.getTotalPage()));
//	                }
//	                else
//	                {
//	                	pageInfo.append("<li class=\"paginItem\"><a disabled=\"disabled\" >后页</a> </li>");
//	                	pageInfo.append("<li class=\"paginItem\"><a disabled=\"disabled\">尾页</a> </li>");
//	                }
//	                //非AJRA出发表单提交 AJRA调用回掉还是
//	                pageInfo.append(MessageFormat.format(" <li class=\"paginItem\" style=\"display:none\">  <input   type=\"text\"  onclick=\"this.select()\" value=\"{0}\" name=\"cpage\" id=\"cpage\" style=\"width:30px;\" /><span>页</span></li>", model.getCpage()));
//	                pageInfo.append(MessageFormat.format(" <li class=\"paginItem\" style=\"display:none\">  <input  type=\"submit\"  value=\"Go\" id=\"mvcpager_btnchk\" onclick=\"return checkInput({0},{1})\" /> </li>", model.getTotalPage(),  0));
//	                pageInfo.append("</div>");
//	                
//	                pageInfo.append("</ul>");   
//	                pageInfo.append("</div>");
//	            }
//	          //  return new HtmlString(pageHtml.ToString());

			out.print(pageInfo.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
		
	}

	private int pageSize=10;//每页大小
	private int cpage;
	private int totalCount;

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCpage() {
		return cpage;
	}
	public void setCpage(int cpage) {
		this.cpage = cpage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
}